package com.my.xq.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import com.my.xq.domain.VoteResultQueryParam;
import com.my.xq.domain.entity.VoteResult;
import com.sskj.core.query.annotation.DynamicQuery;
import com.sskj.core.repository.GenericRepository;

public interface VoteResultRepository extends GenericRepository<VoteResult, String> {
	@DynamicQuery
	Page<VoteResult> findPage(VoteResultQueryParam vqp,Pageable pageable);
	
	@Query(value = "select count(c.doorplate_no) from xq_doorplate c where c.proprietor_id=?1 and "
			+ "c.court_id=?2", nativeQuery = true)
	Integer getVoteCount(String proprietorId,String courtId);
	
	@Query("SELECT r FROM VoteResult r " + " where r.proprietorId = ?1 AND "
			+ "r.courtId=?2 and r.optionId in "
			+ "(SELECT x.id from Option x where x.planid=?3) order by r.createdTime desc")
	List<VoteResult> findOneByCourtAndPerson(String proprietorId,String courtId,String planId);
	
	@Query("SELECT r FROM VoteResult r " + " where r.courtId = ?1 AND "
			+ "r.proprietorId in (SELECT x.id FROM Proprietor x where x.name"
			+ " like CONCAT('%',?2,'%')) AND r.optionId = ?3")
	@DynamicQuery
	Page<VoteResult> searchOption1(String courtId,String proprietorName,String optionId,Pageable pageable);
	
	@Query("SELECT r FROM VoteResult r " + " where "
			+ "r.proprietorId in (SELECT x.id FROM Proprietor x where x.name"
			+ " like CONCAT('%',?1,'%')) AND r.optionId = ?2")
	@DynamicQuery
	Page<VoteResult> searchOption2(String proprietorName,String optionId,Pageable pageable);
}
